<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<body class="gray-bg">
<div class="content">
    <form id="form1" class="query-list" name="form1" method="post" action="">
        <table class="table-query">
            <tr>
                <th>广告联盟名称:</th>
                <td>
                    <select class="easyui-combobox" prompt="请选择" id="advunionid" name="advunionid"></select>
                </td>
                <th>联盟账号:</th>
                <td>
                    <select class="easyui-combobox" prompt="请选择" id="unionaccountid" name="unionaccountid"></select>
                </td>
            </tr>
            <tfoot>
            <td colspan="6">
                <button class="btn btn-info" type="button" onclick="load()">查 询</button>
                <button class="btn btn-warning" onclick="clearAll();" type="button">重 置</button>
            </td>
            </tfoot>
        </table>
    </form>
    <input id="thirdsdkid" name="thirdsdkid" type="hidden" th:value="${thirdsdkid}">
    <div class="result-list">
        <table id="dg" data-mobile-responsive="true">
        </table>
    </div>
    <!--shiro控制bootstraptable行内按钮看见性 来自bootdo的创新方案 -->
    <div>
        <script type="text/javascript">
            var s_edit_h = 'hidden';
            var s_remove_h = 'hidden';
            var s_resetPwd_h = 'hidden';
        </script>
    </div>
    <div shiro:hasPermission="market:thirdSdkAdvunionRelationship:edit">
        <script type="text/javascript">
            s_edit_h = '';
        </script>
    </div>
    <div shiro:hasPermission="market:thirdSdkAdvunionRelationship:remove">
        <script type="text/javascript">
            var s_remove_h = '';
        </script>
    </div>
    <div shiro:hasPermission="market:thirdSdkAdvunionRelationship:resetPwd">
        <script type="text/javascript">
            var s_resetPwd_h = '';
        </script>
    </div>
</div>
<link rel="stylesheet" type="text/css" href="">
<link href="/js/js/easyui/themes/default/easyui.css"
      rel="stylesheet">
<link href="/js/js/easyui/themes/icon.css"
      rel="stylesheet">
<script type="text/javascript" src="/js/js/hyjx.project.js"></script>
<script type="text/javascript" src="/js/js/easyui/jquery.min.js"></script>
<script type="text/javascript" src="/js/js/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/js/js/hyjx.components.js"></script>
<script type="text/javascript" src="/js/js/style.js"></script>
<script type="text/javascript" th:inline="javascript">
    var prefix = "/market/thirdSdkConfigure"
    $(function () {
        $("#advunionid").combobox({
            url: prefix + "/allAdvunionidList",
            valueField: 'id',
            textField: 'db_name',
            editable: false,
            onChange: function () {
                s1change();
            }
        });
        s1change();
        load();
    });

    function s1change() {
        var advunionid = $("#advunionid").combobox("getValue");
        $("#unionaccountid").combobox({
            url: prefix + "/advunionidAccountList?advunionid=" + advunionid,
            valueField: 'value',
            textField: 'name',
            editable: false,
        });
    }

    function clearAll() {
        $("#form1").form("clear");
    }

    var editIndex = undefined;

    function load() {
        var advunionid = $("#advunionid").combobox("getValue");
        var unionaccountid = $("#unionaccountid").combobox("getValue");
        var data_attr = '&thirdsdkid=' + $("#thirdsdkid").val() + '&unionaccountid=' + unionaccountid + '&advunionid=' + advunionid;
        $('#dg').datagrid({
            url: prefix + "/advpositionconfigList",
            queryParams: data_attr,
            pageList: [10, 20, 50, 100, 200],
            fitColumns:
                true,
            singleSelect:
                true,
            rownumbers:
                true,
            pagination:
                true,
            pageNumber:
                1,
            method:
                'post',
            onClickRow:
            onClickRow,
            columns:
                [[{
                    field: 'id',
                    title: '',
                    hidden: true
                },
                    {
                        field: 'advpositionid',
                        title: '广告联盟广告位ID',
                        width: '18%'
                    },
                    {
                        field: 'advpostitionname',
                        title: '广告位名称',
                        width: '20%'
                    },
                    {
                        field: 'advpositiontype',
                        title: '广告位类型',
                        width: '20%',
                        formatter: function (value, row, index) {
                            if (value === 1) {
                                return 'banner';
                            } else if (value === 2) {
                                return '插屏';
                            }
                        }
                    },
                    {
                        field: 'isshowbtn',
                        title: '是否展示close按钮',
                        width: "20%",
                        halign: 'center',
                        editor: {
                            type: 'combobox',
                            options: {
                                url: prefix + "/showWhether",
                                editable: false,
                                valueField: 'value',
                                textField: 'name',
                                panelHeight: 200
                            }
                        },
                        formatter: function (value, rowData, rowIndex) {
                            if (value == 1) {
                                return "<label title='" + "展示" + "'>" + "展示" + "</label>";
                            } else {
                                return "<label title='" + "不展示" + "'>" + "不展示" + "</label>";
                            }
                        }
                    },
                    {
                        field: 'advshownum',
                        title: '比例',
                        width: "20%",
                        halign: 'center',
                        editor: 'numberbox',
                        formatter: function (value, rowData, rowIndex) {
                            if (value != null) {
                                return "<label title='" + value + "'>" + value + "</label>";
                            }
                        }
                    }
                ]],
            toolbar:
                [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function () {
                        if (endEditing()) {
                            if ($('#dg').datagrid('getChanges').length) {
                                var updated1 = $('#dg').datagrid('getChanges', 'updated');
                                if (updated1.length) {
                                    updated1 = JSON.stringify(updated1);
                                }
                                /*var data_attr = '&updated1=' + updated1 + '&thirdsdkid=' + $("#thirdsdkid").val();*/
                                $.ajax({
                                    url: prefix + "/saveAdvpositionconfigList",
                                    type: "POST",
                                    data: {
                                        updated1: updated1,
                                        thirdsdkid: $("#thirdsdkid").val(),
                                    },
                                    success: function (msg) {
                                        if ("true" == msg.errormsg) {
                                            $.messager.alert("提示信息", "保存成功！", "info", function () {
                                                load();
                                            });
                                        } else if ("errorsum" == msg.errormsg) {
                                            $.messager.alert("提示信息", "比例不对", "error", function () {
                                                load();
                                            });
                                        } else {
                                            $.messager.alert("提示信息", msg.errormsg, "error", function () {
                                            });
                                        }
                                    }
                                });
                            }
                        }
                    }
                }, {
                    text: '撤销',
                    iconCls: 'icon-undo',
                    handler: function () {
                        $('#dg').datagrid('rejectChanges');
                        editIndex = undefined;
                    }
                }, {
                    text: '平均分配',
                    iconCls: 'icon-save',
                    handler: function () {
                        var data_attr = '&thirdsdkid=' + $("#thirdsdkid").val();
                        $.ajax({
                            url: prefix + "/saveAdvpositionconfigtAverage",
                            type: "POST",
                            data: data_attr,
                            success: function (msg) {
                                if ("true" == msg.errormsg) {
                                    $.messager.alert("提示信息", "保存成功！", "info", function () {
                                        load();
                                    });
                                } else {
                                    $.messager.alert("提示信息", msg.errormsg, "error", function () {
                                    });
                                }
                            }
                        });
                    }
                }, {
                    text: '全部展示',
                    iconCls: 'icon-save',
                    handler: function () {
                        var data_attr = '&thirdsdkid=' + $("#thirdsdkid").val();
                        $.ajax({
                            url: prefix + "/showAdvpositionconfig",
                            type: "POST",
                            data: data_attr,
                            success: function (msg) {
                                if ("true" == msg.errormsg) {
                                    $.messager.alert("提示信息", "保存成功！", "info", function () {
                                        load();
                                    });
                                } else {
                                    $.messager.alert("提示信息", msg.errormsg, "error", function () {
                                    });
                                }
                            }
                        });
                    }
                }, {
                    text: '全部不展示',
                    iconCls: 'icon-save',
                    handler: function () {
                        var data_attr = '&thirdsdkid=' + $("#thirdsdkid").val();
                        $.ajax({
                            url: prefix + "/noshowAdvpositionconfig",
                            type: "POST",
                            data: data_attr,
                            success: function (msg) {
                                if ("true" == msg.errormsg) {
                                    $.messager.alert("提示信息", "保存成功！", "info", function () {
                                        load();
                                    });
                                } else {
                                    $.messager.alert("提示信息", msg.errormsg, "error", function () {
                                    });
                                }
                            }
                        });
                    }
                }]
        });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    var editIndex = undefined;

    function endEditing() {
        if (editIndex == undefined) {
            return true
        }
        if ($('#dg').datagrid('validateRow', editIndex)) {
            /* var ed = $('#dg').datagrid('getEditor', {index:editIndex,field:'productid'});
            var productname = $(ed.target).combobox('getText');
            $('#dg').datagrid('getRows')[editIndex]['productname'] = productname; */
            $('#dg').datagrid('endEdit', editIndex);
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }

    function onClickRow(index) {
        if (editIndex != index) {
            if (endEditing()) {
                $('#dg').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }

    function add() {
        layer.open({
            type: 2,
            title: '增加',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['800px', '520px'],
            content: prefix + '/add' // iframe的url
        });
    }

    function editapp(id) {
        window.location.href = "/market/thirdSdkAdvunionRelationship?unionaccountid=" + id;
    }

    function editadvposition(id) {
        window.location.href = "/market/thirdSdkAdvunionAdvpositionRelationship?unionaccountid=" + id;
    }


    function remove(id, islock) {
        var actCh;
        if (islock == 0) {
            islock = 1;
            actCh = "确认要开启账号吗？";
        } else {
            islock = 0;
            actCh = "确认要关闭账号吗？";
        }
        layer.confirm(actCh, {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                url: prefix + "/remove",
                type: "post",
                data: {
                    'id': id,
                    'islock': islock
                },
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        })
    }

    function resetPwd(id) {
    }

    function batchRemove() {
        var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
        if (rows.length == 0) {
            layer.msg("请选择要删除的数据");
            return;
        }
        layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
            btn: ['确定', '取消']
            // 按钮
        }, function () {
            var ids = new Array();
            // 遍历所有选择的行数据，取每条数据对应的ID
            $.each(rows, function (i, row) {
                ids[i] = row['id'];
            });
            $.ajax({
                type: 'POST',
                data: {
                    "ids": ids
                },
                url: prefix + '/batchRemove',
                success: function (r) {
                    if (r.code == 0) {
                        layer.msg(r.msg);
                        reLoad();
                    } else {
                        layer.msg(r.msg);
                    }
                }
            });
        }, function () {

        });
    }

</script>
</body>
</html>